<!--
* description: 盘点单财务审核
* author: xuliwen
* createdDate: 2021-01-21
-->
<template>
  <div class="app-container app-container-table">
    <one-table-template
      ref="multipleTable"
      :key="oneTableKey"
      :dynamic-buttons="tableButtons"
      :dynamic-components="tableComponents"
      :dynamic-api-config="apiConfig"
      :dynamic-table-cols="tableCols"
      :dynamic-form-fields="formField"
      :dynamic-is-show-select="true"
      :dynamic-is-init-table="true"
      :dynamic-is-show-more-btn="true"
      :dynamic-api-style="'restful'"
      :callback-obj="callbackObj"
    />

    <edit
      :key="editPopupsKey"
      :selectiondata="selectiondata"
      :popups-visible="editPopupsVisible"
      :popups-state="editPopupsState"
      :dynamic-api-style="'restful'"
      @close="close"
    />
  </div>
</template>
<script>
import { oneTableWithViewTemplateMixins } from '@/components/mixins/oneTableWithViewTemplateMixins'
import OneTableTemplate from '@/components/templates/oneTable'
import api from '@/api/pa/apiBranchWarehouse'
import Vue from 'vue'
import request from '@/utils/request'

export default {
  name: 'InventoryVerify',

  components: {
    OneTableTemplate,
    Edit: () => import('./edit')
  },
  // 组件混入对象
  mixins: [oneTableWithViewTemplateMixins],

  // 阻塞路由预加载网格中组件的数据
  beforeRouteEnter(to, from, next) {
    Vue.prototype.$ConfigCache.CacheConfig.initData([to.path, ''], function() {
      next()
    })
  },

  data() {
    // 页面标识（与上面beforeRouteEnter中的对应）
    const viewPageCode = this.$route.path

    // 绑定事件配置化事件
    this.$ConfigCache.CacheConfig.bindEvent(this, [viewPageCode])

    return {
      icon: 'el-icon-arrow-down',
      status: false,
      editRowData: {},
      selectiondata: [],
      // 网格查询API配置对象
      apiConfig: api.queryCheckStoreAuditInfo,
      // 动态组件-按钮
      tableButtons: [
        {
          compKey: 'btnKey1',
          type: 'primary',
          size: 'small',
          name: 'search',
          clickEvent: () => this.queryTable(1),
          text: this.$t('sys.button.query'), // 查询
          position: 'right',
          fuzzySearch: true
        },
        {
          compKey: 'btnKey2',
          type: '',
          size: 'small',
          name: 'reset',
          clickEvent: () => this.reset(),
          text: this.$t('sys.button.reset'),
          position: 'right' // 重置
        },
        {
          compKey: 'btnKey4',
          disabled: true,
          parentFileds: 'disabled:table',
          type: '',
          size: 'small',
          clickEvent: () => this.pass('0'),
          text: '驳回', // err
          name: 'reject',
          position: 'left'
        },
        {
          compKey: 'btnKey4',
          disabled: true,
          type: '',
          size: 'small',
          name: 'adopt',
          clickEvent: () => this.pass('1'),
          text: '通过', // 通过
          position: 'left'
        },
        {
          compKey: 'btnKey4',
          disabled: true,
          type: '',
          size: 'small',
          name: 'adopt',
          clickEvent: () => this.pass('2'),
          text: '驳回完成', // 驳回完成
          position: 'left'
        }
      ],

      // 动态组件-查询条件
      tableComponents:
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path] &&
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path]
          .tableComponents &&
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path]
          .tableComponents.length > 0
          ? this.$ConfigCache.CacheConfig.cacheData[this.$route.path]
            .tableComponents
          : [
            {
              compKey: 'compKey1',
              labelName: '盘点单号：',
              codeField: 'checkStoreCode',
              component: () => import('@/components/org/commonInput'),
              type: 'inputText',
              isMul: false,
              isMust: true
            },
            {
              compKey: 'compKey2',
              labelName: '盘点方式：',
              codeField: 'checkModel',
              component: () => import('@/components/org/LookupValue'),
              oFields: 'statusId,statusText',
              options: [
                { statusId: '2', statusText: '抽盘' },
                { statusId: '1', statusText: '全盘' }
              ],
              clearable: false,
              isMul: false,
              isMust: true
            },
            {
              compKey: 'compKey3',
              labelName: '盘点人：',
              codeField: 'checker',
              component: () => import('@/components/org/commonInput'),
              type: 'inputText',
              isMul: false,
              isMust: true
            },
            {
              compKey: 'compKey4',
              labelName: '盘点日期：',
              codeField: 'beginDate,endDate',
              component: () =>
                import('@/components/org/datePicker/andtDatePicker'),
              type: 'YYYY-MM-DD HH:mm:ss',
              // type: 'twoDatePicker',
              dataToObject: { timer: true, thirtyTime: true },
              hhmmss: true, // 默认有时分秒
              format: 'yyyy-MM-dd',
              isMul: false,
              isMust: true
            },
            {
              compKey: 'compKey5',
              labelName: '过滤条件：',
              codeField: 'filtration',
              component: () => import('@/components/org/LookupValue'),
              oFields: 'statusId,statusText',
              options: [
                { statusId: '1', statusText: '只显示相符' },
                { statusId: '2', statusText: '只显示盘盈' },
                { statusId: '3', statusText: '只显示盘亏' },
                { statusId: '4', statusText: '只显示差异' },
                { statusId: '5', statusText: '只显示有物无账' }
              ],
              type: 'dropdownList',
              isMul: false,
              isMust: false
            },
            {
              compKey: 'compKey6',
              labelName: '备件编码：',
              codeField: 'partNo',
              component: () => import('@/components/org/commonInput'),
              type: 'inputText',
              isMul: false,
              isMust: false
            },
            {
              compKey: 'compKey7',
              labelName: '备件名称：',
              codeField: 'partName',
              component: () => import('@/components/org/commonInput'),
              type: 'inputText',
              isMul: false,
              isMust: false
            }
          ],

      // 动态生成网格列
      tableCols:
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path] &&
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path].tableCols &&
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path].tableCols
          .length > 0
          ? this.$ConfigCache.CacheConfig.cacheData[this.$route.path].tableCols
          : [
            {
              prop: 'releaseId',
              label: '主键ID',
              hidden: true,
              alwaysHidden: true
            },
            {
              prop: 'updateControlId',
              label: '并发控制ID',
              hidden: true,
              alwaysHidden: true
            },
            {
              prop: 'bigAreaId',
              label: '大区ID',
              hidden: true,
              alwaysHidden: true
            },
            {
              prop: 'checkStoreCode',
              label: '盘点单号'
            },
            {
              prop: 'partNo',
              label: '备件编码'
            },

            {
              prop: 'partName',
              label: '备件名称'
            },
            {
              prop: 'warehouseName',
              label: '仓库名称',
              width: '150px'
            },
            {
              prop: 'placeId',
              label: '仓位编码',
              width: '100px'
            },
            {
              prop: 'storageQty',
              label: '账面数量'
            },
            {
              prop: 'factQty',
              label: '盘点实数'
            },

            {
              prop: 'profitQty',
              label: '盈亏数量'
            },
            {
              prop: 'remark',
              label: '盈亏原因'
            },
            {
              prop: 'lastEditDate',
              label: '盘点实数导入时间',
              width: '150px'
            },
            {
              prop: 'unit',
              label: '计量单位'
            }
          ],
      callbackObj: {
        // 手动勾选时
        onCheckSelect: (selection, row) => {
          this.selectiondata = selection
        },
        onCheckSelectChange: (selection) => {
          this.onCheckSelectChange1(selection)
        }
      },
      // 表单查询数据
      formField: this.$utils.getFormField(
        this,
        {
          checkStoreCode: '',
          checkModel: '2',
          checker: '',
          beginDate: '',
          endDate: '',
          filtration: '',
          partNo: '',
          partName: ''
        },
        this.$ConfigCache.CacheConfig.cacheData[viewPageCode]
      )
    }
  },
  watch: {
    'formField.orderStatus': {
      deep: true,
      handler: function(newV, oldV) {
        console.log('watch中：', newV)
        // this.querybystatus(newV)
        this.queryTable()
      }
    }
  },
  mounted() {
    // 列表查询
    this.queryTable(1)
  },

  methods: {
    // 订单状态切换
    togglestatus() {
      if (this.status === true) {
        this.icon = 'el-icon-arrow-down'
        this.status = false
      } else {
        this.icon = 'el-icon-arrow-up'
        this.status = true
      }
      this.$refs.multipleTable.$refs.compKey11[0].curIsShow = this.status
      this.$refs.multipleTable.setTableHeight()
    },
    onCheckSelectChange1(selection) {
      var btn1 = this.tableButtons[2]
      var btn2 = this.tableButtons[3]
      var btn3 = this.tableButtons[4]
      const isCheck = selection && selection.length > 0
      btn1.disabled = !isCheck
      btn2.disabled = !isCheck
      btn3.disabled = !isCheck
    },
    // 批量驳回、通过
    pass(type) {
      if (type === '0') {
        this.editPopupsState = 'edit'
        this.editPopupsKey = 'popkey' + this.$utils.generateId()
        this.editPopupsVisible = true
      } else if (type === '1') {
        this.editPopupsKey = 'popkey' + this.$utils.generateId()
        this.editPopupsState = 'add'
        this.editPopupsVisible = true
      } else {
        request({
          url: api.auditRejectEnd.APIUrl,
          method: 'post',
          data: this.selectiondata
        }).then((res) => {
          this.$message.success(res.msg)
          this.queryTable(1)
        })
      }
    }
  }
}
</script>
<style lang='scss' scoped>
.red {
  background: red;
}
</style>
